package com.xbongbong.saas.service.impl;

import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.DeleteDTO;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.service.toolbox.statistic.help.ChartQueryHelp;
import com.xbongbong.pro.service.toolbox.statistic.help.FormHelp;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.domain.entity.FundAccountFlowEntity;
import com.xbongbong.saas.model.FundAccountFlowModel;
import com.xbongbong.saas.service.FundAccountFlowService;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.termQuery;

/**
 * @author zcp
 * @version v1.0
 * @since v1.0
 */
@Service("fundAccountFlowService")
public class FundAccountFlowServiceImpl implements FundAccountFlowService {
    @Resource
    private FundAccountFlowModel fundAccountFlowModel;
    @Resource
    private ChartQueryHelp chartQueryHelp;
    @Resource
    private FormHelp formHelp;
    @Resource
    private PaasEsModel paasEsModel;

    @Override
    public void deleteBatch(List<Long> sheetIdIn, Integer type, String corpid) throws XbbException {
        if (CollectionUtils.isEmpty(sheetIdIn)) {
            return;
        }
        BoolQueryBuilder boolQueryBuilder = chartQueryHelp.boolQueryBuilder4Base(corpid);
        boolQueryBuilder.filter(termQuery("type", type));
        List<FundAccountFlowEntity> result = formHelp.getFormListByIds4All(IndexTypeEnum.IDX_FUND_ACCOUNT_FLOW,boolQueryBuilder, "sheetId", sheetIdIn, null, FundAccountFlowEntity.class);
        if (result != null && result.size() > 0) {
            result.forEach(e -> {
                e.setDel(DelEnum.DELETE.getDel());
                e.setUpdateTime(DateTimeUtil.getInt());
            });
            fundAccountFlowModel.updateBatch(result, corpid);
        }
    }

    @Override
    public void addRollback(String corpid, Integer type, List<Long> sheetIdIn) throws XbbException {
        if (CollectionsUtil.isEmpty(sheetIdIn)) {
            return;
        }
        BoolQueryBuilder boolQueryBuilder = chartQueryHelp.boolQueryBuilder4Base(corpid);
        boolQueryBuilder.filter(termQuery("type", type));
        List<FundAccountFlowEntity> result = formHelp.getFormListByIds4All(IndexTypeEnum.IDX_FUND_ACCOUNT_FLOW,boolQueryBuilder, "sheetId", sheetIdIn, Collections.singletonList(BasicConstant.ID), FundAccountFlowEntity.class);
        if (CollectionsUtil.isNotEmpty(result)) {
            //新建失败，则直接删除es
            List<DeleteDTO> deleteUserList = new ArrayList<>();
            for (FundAccountFlowEntity entity : result) {
                DeleteDTO deleteDTO = new DeleteDTO();
                deleteDTO.setEsId(corpid + "_" + entity.getId());
                deleteDTO.setIndexTypeEnum(IndexTypeEnum.IDX_FUND_ACCOUNT_FLOW);
                deleteUserList.add(deleteDTO);
            }
            paasEsModel.physicalDeleteBatchChild(deleteUserList, corpid, IndexTypeEnum.IDX_FUND_ACCOUNT_FLOW);
        }
    }

    @Override
    public void updateRollback(String corpid, Integer type, List<Long> sheetIdIn) throws XbbException {
        if (CollectionUtils.isEmpty(sheetIdIn)) {
            return;
        }
        //从数据库读取数据，同步至es
        List<FundAccountFlowEntity> result =fundAccountFlowModel.getFlowListByType(corpid, type, sheetIdIn);
        if (CollectionUtils.isNotEmpty(result)) {
            fundAccountFlowModel.updateBatchEs(result);
        }
    }
}
